home *** CD-ROM | disk | FTP | other *** search
/ Aminet 40 / Aminet 40 (2000)(Schatztruhe)[!][Dec 2000].iso / Aminet / dev / lang / Python16_Src.lha / Python16_Source / Modules / newmodule.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-09-10  |  4.7 KB  |  205 lines

  1. /* Module new -- create new objects of various types */
  2.  
  3. #include "Python.h"
  4. #include "compile.h"
  5. #include "protos/newmodule.h"
  6.  
  7. static char new_instance_doc[] =
  8. "Create an instance object from (CLASS, DICT) without calling its __init__().";
  9.  
  10. static PyObject *
  11. new_instance(unused, args)
  12.     PyObject* unused;
  13.     PyObject* args;
  14. {
  15.     PyObject* klass;
  16.     PyObject *dict;
  17.     PyInstanceObject *inst;
  18.     if (!PyArg_ParseTuple(args, "O!O!:instance",
  19.                   &PyClass_Type, &klass,
  20.                   &PyDict_Type, &dict))
  21.         return NULL;
  22.     inst = PyObject_New(PyInstanceObject, &PyInstance_Type);
  23.     if (inst == NULL)
  24.         return NULL;
  25.     Py_INCREF(klass);
  26.     Py_INCREF(dict);
  27.     inst->in_class = (PyClassObject *)klass;
  28.     inst->in_dict = dict;
  29.     return (PyObject *)inst;
  30. }
  31.  
  32. static char new_im_doc[] =
  33. "Create a instance method object from (FUNCTION, INSTANCE, CLASS).";
  34.  
  35. static PyObject *
  36. new_instancemethod(unused, args)
  37.     PyObject* unused;
  38.     PyObject* args;
  39. {
  40.     PyObject* func;
  41.     PyObject* self;
  42.     PyObject* classObj;
  43.  
  44.     if (!PyArg_ParseTuple(args, "OOO!:instancemethod",
  45.                   &func,
  46.                   &self,
  47.                   &PyClass_Type, &classObj))
  48.         return NULL;
  49.     if (!PyCallable_Check(func)) {
  50.         PyErr_SetString(PyExc_TypeError,
  51.                 "first argument must be callable");
  52.         return NULL;
  53.     }
  54.     if (self == Py_None)
  55.         self = NULL;
  56.     else if (!PyInstance_Check(self)) {
  57.         PyErr_SetString(PyExc_TypeError,
  58.                 "second argument must be instance or None");
  59.         return NULL;
  60.     }
  61.     return PyMethod_New(func, self, classObj);
  62. }
  63.  
  64. static char new_function_doc[] =
  65. "Create a function object from (CODE, GLOBALS, [NAME, ARGDEFS]).";
  66.  
  67. static PyObject *
  68. new_function(unused, args)
  69.     PyObject* unused;
  70.     PyObject* args;
  71. {
  72.     PyObject* code;
  73.     PyObject* globals;
  74.     PyObject* name = Py_None;
  75.     PyObject* defaults = Py_None;
  76.     PyFunctionObject* newfunc;
  77.  
  78.     if (!PyArg_ParseTuple(args, "O!O!|SO!:function",
  79.                   &PyCode_Type, &code,
  80.                   &PyDict_Type, &globals,
  81.                   &name,
  82.                   &PyTuple_Type, &defaults))
  83.         return NULL;
  84.  
  85.     newfunc = (PyFunctionObject *)PyFunction_New(code, globals);
  86.     if (newfunc == NULL)
  87.         return NULL;
  88.  
  89.     if (name != Py_None) {
  90.         Py_XINCREF(name);
  91.         Py_XDECREF(newfunc->func_name);
  92.         newfunc->func_name = name;
  93.     }
  94.     if (defaults != Py_None) {
  95.         Py_XINCREF(defaults);
  96.         Py_XDECREF(newfunc->func_defaults);
  97.         newfunc->func_defaults  = defaults;
  98.     }
  99.  
  100.     return (PyObject *)newfunc;
  101. }
  102.  
  103. static char new_code_doc[] =
  104. "Create a code object from (ARGCOUNT, NLOCALS, STACKSIZE, FLAGS, CODESTRING, CONSTANTS, NAMES, VARNAMES, FILENAME, NAME, FIRSTLINENO, LNOTAB).";
  105.  
  106. static PyObject *
  107. new_code(unused, args)
  108.     PyObject* unused;
  109.     PyObject* args;
  110. {
  111.     int argcount;
  112.     int nlocals;
  113.     int stacksize;
  114.     int flags;
  115.     PyObject* code;
  116.     PyObject* consts;
  117.     PyObject* names;
  118.     PyObject* varnames;
  119.     PyObject* filename;
  120.     PyObject* name;
  121.     int firstlineno;
  122.     PyObject* lnotab;
  123.     PyBufferProcs *pb;
  124.  
  125.     if (!PyArg_ParseTuple(args, "iiiiOO!O!O!SSiS:code",
  126.                   &argcount, &nlocals, &stacksize, &flags,
  127.                   &code,
  128.                   &PyTuple_Type, &consts,
  129.                   &PyTuple_Type, &names,
  130.                   &PyTuple_Type, &varnames,
  131.                   &filename, &name,
  132.                   &firstlineno, &lnotab))
  133.         return NULL;
  134.  
  135.     pb = code->ob_type->tp_as_buffer;
  136.     if (pb == NULL ||
  137.         pb->bf_getreadbuffer == NULL ||
  138.         pb->bf_getsegcount == NULL ||
  139.         (*pb->bf_getsegcount)(code, NULL) != 1)
  140.     {
  141.         PyErr_SetString(PyExc_TypeError,
  142.           "bytecode object must be a single-segment read-only buffer");
  143.         return NULL;
  144.     }
  145.  
  146.     return (PyObject *)PyCode_New(argcount, nlocals, stacksize, flags,
  147.                       code, consts, names, varnames,
  148.                       filename, name, firstlineno, lnotab);
  149. }
  150.  
  151. static char new_module_doc[] =
  152. "Create a module object from (NAME).";
  153.  
  154. static PyObject *
  155. new_module(unused, args)
  156.     PyObject* unused;
  157.     PyObject* args;
  158. {
  159.     char *name;
  160.   
  161.     if (!PyArg_ParseTuple(args, "s:module", &name))
  162.         return NULL;
  163.     return PyModule_New(name);
  164. }
  165.  
  166. static char new_class_doc[] =
  167. "Create a class object from (NAME, BASE_CLASSES, DICT).";
  168.  
  169. static PyObject *
  170. new_class(unused, args)
  171.     PyObject* unused;
  172.     PyObject* args;
  173. {
  174.     PyObject * name;
  175.     PyObject * classes;
  176.     PyObject * dict;
  177.   
  178.     if (!PyArg_ParseTuple(args, "SO!O!:class", &name, &PyTuple_Type, &classes,
  179.                   &PyDict_Type, &dict))
  180.         return NULL;
  181.     return PyClass_New(classes, dict, name);
  182. }
  183.  
  184. static PyMethodDef new_methods[] = {
  185.     {"instance",        new_instance,        1, new_instance_doc},
  186.     {"instancemethod",    new_instancemethod,    1, new_im_doc},
  187.     {"function",        new_function,        1, new_function_doc},
  188.     {"code",        new_code,        1, new_code_doc},
  189.     {"module",        new_module,        1, new_module_doc},
  190.     {"classobj",        new_class,        1, new_class_doc},
  191.     {NULL,            NULL}        /* sentinel */
  192. };
  193.  
  194. char new_doc[] =
  195. "Functions to create new objects used by the interpreter.\n\
  196. \n\
  197. You need to know a great deal about the interpreter to use this!";
  198.  
  199. DL_EXPORT(void)
  200. initnew()
  201. {
  202.     Py_InitModule4("new", new_methods, new_doc, (PyObject *)NULL,
  203.                PYTHON_API_VERSION);
  204. }
  205.